package com.gjszn.kh.service;

import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gjszn.kh.context.MemberRCodeContext;
import com.gjszn.kh.dao.RecommendationCodeMapper;
import com.gjszn.kh.model.MyBatisParameterBean;
import com.gjszn.kh.model.RecommendationCode;

/**    
* @Title: RecommendationCodeSerivce.java  
* @Package com.gjszn.kh.service    
* @author jys@nx-yt.com    
* @date 2016年1月5日 下午5:14:22  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Service
public class RecommendationCodeSerivce {
	private static Logger logger = LogManager.getRootLogger();
	
	@Inject
	private RecommendationCodeMapper recommendationCodeMapper;
	
	public List<RecommendationCode> getAllByMember(String number){
		return recommendationCodeMapper.selectAllByMember(number);
	}
	
	public List<RecommendationCode> getAll(){
		return recommendationCodeMapper.selectAll();
	}
	
	public RecommendationCode getByMemberNoAndCode(String number, String code){
		return recommendationCodeMapper.selectByMemberNoAndCode(number, code);
	}
	
	public boolean add(RecommendationCode recommendationCode){
		RecommendationCode rCode = getByMemberNoAndCode(recommendationCode.getMember(), recommendationCode.getCode());
		if(rCode != null){
			logger.info(recommendationCode.getMember() + "的推荐码" + recommendationCode.getCode() + ":已被注册了");
			return  false;
		}
		if(recommendationCodeMapper.insertSelective(recommendationCode) == 1){
			logger.info(recommendationCode.getName() + ":插入成功");
			// 将推荐码缓存起来
			MemberRCodeContext.add(recommendationCode);
			return  true;
		}
		logger.info(recommendationCode.getName() + ":信息插入失败");
		return  false;
	}
	
	public boolean addOrUpdate(RecommendationCode recommendationCode){
		if(getById(recommendationCode.getId()) != null){
			return update(recommendationCode);
		}
		
		recommendationCode.setId(null);
		if(recommendationCodeMapper.insertSelective(recommendationCode) == 1){
			logger.info(recommendationCode.getName() + ":插入成功");
			return  true;
		}
		logger.info(recommendationCode.getName() + ":信息插入失败");
		return  false;
	}
	
	public boolean update(RecommendationCode recommendationCode){
		if(recommendationCodeMapper.updateByPrimaryKeySelective(recommendationCode) == 1){
			logger.info(recommendationCode.getName() + ":信息修改成功");
			// 将推荐码缓存起来
			MemberRCodeContext.update(recommendationCode);
			return  true;
		}
		logger.info(recommendationCode.getName() + ":信息修改失败");
		return  false;
	}
	
	public RecommendationCode getById(int id){
		return recommendationCodeMapper.selectByPrimaryKey(id);
	}
	
	public boolean delete(int id){
		RecommendationCode rCode = getById(id);
		if(rCode!=null && recommendationCodeMapper.deleteByPrimaryKey(id) == 1){
			logger.info("推荐码[" + id +"]信息删除成功");
			// 将推荐码缓存起来
			MemberRCodeContext.delete(rCode);
			return true;
		}
		logger.info("推荐码[" + id +"]信息删除失败");
		return true;
	}
	
	public RecommendationCode load(String member, String code){
		return recommendationCodeMapper.selectByMemberNoAndCode(member, code);
	}
	
	public List<RecommendationCode> listForPage(String number, int page, int rows){
		int count = rows>0 ? rows : 10;
		int start = page > 1 ? (page-1)*rows : 0;
		return recommendationCodeMapper.selectForPageByNumber(number, start, count);
	}
	
	public List<RecommendationCode> listForPage(String sortColumnName, boolean isAsc){
		String dir = isAsc ? "ASC":"DESC";
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(sortColumnName, dir, null, null);
		return recommendationCodeMapper.selectForPage(batisParameterBean);
	}
	
	public List<RecommendationCode> listForPage(int page, int rows){
		int count = rows>0 ? rows : 10;
		int start = page > 1 ? (page-1)*rows : 0;
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(null, null, start, count);
		return recommendationCodeMapper.selectForPage(batisParameterBean);
	}
	
	public List<RecommendationCode> listForPage(String sortColumnName, boolean isAsc, int page, int rows){
		String dir = isAsc ? "ASC":"DESC";
		int count = rows>0 ? rows : 10;
		int start = page > 1 ? (page-1)*rows : 0;
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(sortColumnName, dir, start, count);
		return recommendationCodeMapper.selectForPage(batisParameterBean);
	}
	
	public int total(String member){
		return recommendationCodeMapper.total(member);
	}
}

